<!-- eslint-disable vue/multi-word-component-names -->
<template>
  <div class="login-container">
    <el-card class="login-card">
      <template #header>
        <div class="card-header">
          <span>管理员登录</span>
        </div>
      </template>
      <el-form ref="loginForm" class="login-form">
        <el-form-item prop="username" :rules="rules.username">
          <el-input v-model="username" placeholder="请输入用户名" :validate-event="false">
            <template #prefix>
              <User class="el-input__icon" />
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password" :rules="rules.password">
          <el-input v-model="password" type="password" placeholder="请输入密码" :validate-event="false">
            <template #prefix>
              <Lock class="el-input__icon" />
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleLogin" style="width: 100%" :loading="loading">登录</el-button>
        </el-form-item>
      </el-form>
      <div class="tips">默认账号: admin 密码: admin123</div>
      <div v-if="errorMessage" class="error-message">{{ errorMessage }}</div>
    </el-card>
  </div>
</template>

<script setup>
import { defineOptions } from 'vue'

defineOptions({
  name: 'LoginView'
})

// eslint-disable-next-line vue/multi-word-component-names
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { User, Lock } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import axios from 'axios'

const router = useRouter()
const username = ref('')
const password = ref('')
const errorMessage = ref('')
const loading = ref(false)
const rules = {
  username: [{ required: true, message: '请输入用户名', trigger: ['blur', 'change'] }],
  password: [{ required: true, message: '请输入密码', trigger: ['blur', 'change'] }]
}

// 添加防抖函数
function debounce(fn, delay = 300) {
  let timer = null
  return function(...args) {
    if (timer) clearTimeout(timer)
    timer = setTimeout(() => {
      fn.apply(this, args)
    }, delay)
  }
}

// 优化登录处理函数
const handleLogin = debounce(async () => {
  try {
    loading.value = true
    const response = await axios.post('/api/login', {
      username: username.value,
      password: password.value
    })
    
    if (response.data.success) {
      // 存储token和管理员信息到本地
      localStorage.setItem('token', response.data.token)
      localStorage.setItem('adminInfo', JSON.stringify(response.data.admin))
      ElMessage.success('登录成功')
      router.push('/dashboard')
    } else {
      errorMessage.value = response.data.message || '用户名或密码错误'
      setTimeout(() => {
        errorMessage.value = ''
      }, 3000)
    }
  } catch (error) {
    errorMessage.value = '网络错误，请稍后重试'
    console.error('Login error:', error)
    setTimeout(() => {
      errorMessage.value = ''
    }, 3000)
  } finally {
    loading.value = false
  }
});
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-color: #f5f7fa;
}
.login-card {
  width: 400px;
  box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
}
.card-header {
  display: flex;
  justify-content: center;
  font-size: 18px;
  font-weight: bold;
}
.login-form {
  padding: 20px;
}
.tips {
  text-align: center;
  color: #909399;
  font-size: 12px;
  margin-top: -10px;
  margin-bottom: 15px;
}
.error-message {
  color: #f56c6c;
  font-size: 12px;
  text-align: center;
  margin-top: 10px;
}
</style>